﻿Namespace World

    Public Class Region

        Private G As GraphicsDeviceManager
        Private ContentDir As String
        Public TerrainID As Integer
        Public LightDirection As Vector3
        Public AmbientLightValue As Single
        Public WindForce As Single = 0.001
        Public WindDirection As Vector3
        Public WaterHeight As Single
        Private Terrains(0) As Processors.Terrain
        Private MapObjects(0) As MapObjects.Models
        Private Player As MapObjects.Player
        Private Sky As MapObjects.SkyDome
        Private Camera As Cameras.FirstPerson

        Public Sub New(Graphics As GraphicsDeviceManager, ContentDirectory As String)
            G = Graphics
            ContentDir = ContentDirectory
        End Sub

        Public Sub Load()
            Camera = New Cameras.FirstPerson(G, New Vector3(100, 50, -100))
            Terrains(TerrainID) = New Processors.Terrain(G)
            LoadTerrainTextures()
            LoadWaterTextures()
            Terrains(TerrainID).Water = New Vector4(0, 0, 128, 128)
            Terrains(TerrainID).AmbientLightValue = 0.4
            AmbientLightValue = 0.4
            Terrains(TerrainID).LightDirection = New Vector3(-0.5, -1, -0.5)
            LightDirection = New Vector3(-0.5, -1, -0.5)
            Terrains(TerrainID).WindForce = 0.001
            WindForce = 0.001
            Terrains(TerrainID).WindDirection = New Vector3(0, 1, 0)
            WindDirection = New Vector3(0, 1, 0)
            Terrains(TerrainID).WaterHeight = 5
            WaterHeight = 5
            Terrains(TerrainID).Load()
            Sky = New MapObjects.SkyDome(G, ContentDir)
            Player = New MapObjects.Player(G, ContentDir)
            Player.ModelLocation = New Vector3(0, 6, 30)
            Player.ModelRotation = 0
            'Player.ImportOBJFile("Player\HumanFemale_2b", 1)
            Player.ImportCOLLADAFile("Player\HobgoblinMale", 1)
            Player.InitializePlayer()
            'Player.SetStaticModelRotation(New Vector3(0, MathHelper.PiOver4, 0))
            MapObjects(0) = New MapObjects.Models(G, ContentDir)
            MapObjects(0).ModelLocation = New Vector3(0, 6, 30)
            MapObjects(0).ImportOBJFile("MapObjects\SimpleBridge", 2)
            MapObjects(0).SetStaticModelRotation(New Vector3(0, 0, 0))
        End Sub

        Private Sub LoadTerrainTextures()
            Dim FS As System.IO.FileStream
            FS = New System.IO.FileStream(ContentDir & "Terrain\grass.jpg", IO.FileMode.Open)
            Terrains(TerrainID).GrassTexture = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
            FS = New System.IO.FileStream(ContentDir & "Terrain\rock.jpg", IO.FileMode.Open)
            Terrains(TerrainID).RockTexture = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
            FS = New System.IO.FileStream(ContentDir & "Terrain\sand.jpg", IO.FileMode.Open)
            Terrains(TerrainID).SandTexture = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
            FS = New System.IO.FileStream(ContentDir & "Terrain\snow.jpg", IO.FileMode.Open)
            Terrains(TerrainID).SnowTexture = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
            FS = New System.IO.FileStream(ContentDir & "Terrain\HeightMap2.jpg", IO.FileMode.Open)
            Terrains(TerrainID).HeightMap = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
        End Sub

        Private Sub LoadWaterTextures()
            Dim FS As System.IO.FileStream
            FS = New System.IO.FileStream(ContentDir & "Terrain\WaterSample.jpg", IO.FileMode.Open)
            'Terrains(TerrainID).WaterBumpMap = New Texture2D(G.GraphicsDevice, G.GraphicsDevice.Viewport.Width, G.GraphicsDevice.Viewport.Height)
            Terrains(TerrainID).WaterBumpMap = Texture2D.FromStream(G.GraphicsDevice, FS)
            FS.Close()
        End Sub

        Public Sub Update(ByVal gameTime As GameTime, ServerTime As DateTime)
            Dim timeDiff As Single, tmpDate As New DateTime(Now.Year, Now.Month, Now.Day, 19, 30, 0)
            timeDiff = CSng(gameTime.ElapsedGameTime.TotalMilliseconds / 1000)
            Camera.Update(timeDiff, Keyboard.GetState, Mouse.GetState)
            Sky.Update(ServerTime)
            Player.Update(timeDiff, Keyboard.GetState)
        End Sub

        Public Sub Draw(ByVal gameTime As GameTime, ByRef Device2 As GraphicsDevice, ByRef Effect As Effect)
            Dim i As Integer
            Device2.Clear(Color.CornflowerBlue)
            Terrains(TerrainID).DrawRefractionMap(Device2, Effect, Camera, Sky)
            Terrains(TerrainID).DrawReflectionMap(Device2, Effect, Camera, Sky, MapObjects)
            Device2.Clear(ClearOptions.Target, Color.CornflowerBlue, 1, 0)
            Sky.Draw(Device2, Effect, Camera, LightDirection, AmbientLightValue)
            Terrains(TerrainID).DrawTerrain(Device2, Effect, Camera.GetViewMatrix, Camera.GetProjMatrix)
            Terrains(TerrainID).DrawWater(gameTime, Device2, Effect, Camera)
            For i = 0 To MapObjects.Count - 1
                MapObjects(i).Draw(Device2, Effect, Camera, LightDirection, AmbientLightValue, False)
            Next i
            Player.Draw(Device2, Effect, Camera, LightDirection, AmbientLightValue, False)
            Device2.SetVertexBuffer(Nothing)
            Device2.Indices = Nothing
        End Sub




    End Class

End Namespace

